package com.demo.controller;

import com.demo.service.ZkNodeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.zookeeper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.io.IOException;

@Slf4j
@RestController
@RequestMapping("/zk")
@Api(description = "zookeeper节点crud")
public class ZkNodeController extends BaseController {

    @Autowired
    private ZkNodeService zkNodeService;

    @ApiOperation(value = "同步获取子节点数据")
    @PostMapping(value = "/getNodeData_sync")
    public Object getNodeData_sync(){
        try {
            zkNodeService.getNodeData_sync();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (KeeperException e) {
            e.printStackTrace();
        }
        return success("");
    }

    @ApiOperation(value = "异步获取子节点")
    @PostMapping(value = "/getNode_async")
    public Object getNode_async(){
        try {
            zkNodeService.getNode_async();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (KeeperException e) {
            e.printStackTrace();
        }
        return success("");
    }

    @ApiOperation(value = "同步获取子节点")
    @PostMapping(value = "/getNode_sync")
    public Object ephemeral_getNode_sync(){
        try {
            zkNodeService.getNode_sync();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (KeeperException e) {
            e.printStackTrace();
        }
        return success("");
    }

    @ApiOperation(value = "异步创建持久节点")
    @PostMapping(value = "/persistent_create_async")
    public Object persistent_create_async(){
        try {
            zkNodeService.persistent_create_async();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (KeeperException e) {
            e.printStackTrace();
        }
        return success("");
    }

    @ApiOperation(value = "同步创建持久节点")
    @PostMapping(value = "/persistent_create_sync")
    public Object persistent_create_sync(){
        try {
            zkNodeService.persistent_create_sync();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (KeeperException e) {
            e.printStackTrace();
        }
        return success("");
    }

    @ApiOperation(value = "异步创建临时节点")
    @PostMapping(value = "/ephemeral_create_async")
    public Object create_async(){
        try {
            zkNodeService.ephemeral_create_async();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return success("");
    }

    @ApiOperation(value = "同步创建临时节点")
    @PostMapping(value = "/ephemeral_create_sync")
    public Object create_sync(){
        try {
            zkNodeService.ephemeral_create_sync();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (KeeperException e) {
            e.printStackTrace();
        }
        return success("");
    }

}
